package com.syq.controller;

import com.syq.pojo.DtsOrderGoods;
import com.syq.service.DtsOrderGoodsService;
import com.syq.service.DtsOrderService;
import com.syq.service.DtsUserService;
import com.syq.util.ResponseUtil;
import com.syq.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

/**
 * @ClassName: AdminStatController
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/28 16:54
 */

@RestController
@CrossOrigin
@RequestMapping("/admin/stat")
public class AdminStatController {
    @Autowired
    private DtsUserService userService;
    @Autowired
    private DtsOrderService orderService;
    @Autowired
    private DtsOrderGoodsService dtsOrderGoodsService;

    @GetMapping("/user")
    public Object user(){
       List<DayStatis> dtsUserList = userService.findUserCnt();
       StatVo statVo=new StatVo();
       String[] columns={"day","users"};
       statVo.setColumns(columns);
       List<Map> map=new ArrayList<>();
        for (DayStatis dayStatis : dtsUserList) {
            Map<String, Object> day=new HashMap<>();
            day.put("day",dayStatis.getDayStr());
            day.put("users",dayStatis.getCnts());
            map.add(day);
        }
        statVo.setRows(map);
        return ResponseUtil.ok(statVo);
    }

    @GetMapping("/order")
    public Object order(){
       List<OrderStat> orderStatList = orderService.findOrderStat();
       StatVo statVo=new StatVo();
       String[] columns={"day","orders","customers","amount","pcr"};
       statVo.setColumns(columns);
       List<Map> map=new ArrayList<>();
        for (OrderStat orderStat : orderStatList) {
            Map<String, Object> day=new HashMap<>();
            day.put("day",orderStat.getDay());
            day.put("orders",orderStat.getOrders());
            day.put("customers",orderStat.getCustomers());
            day.put("amount",orderStat.getAmount());
            day.put("pcr",orderStat.getPcr());
            map.add(day);
        }
        statVo.setRows(map);
        return ResponseUtil.ok(statVo);
    }

    @GetMapping("/goods")
    public Object goods(){
        List<OrderGoodsStat> orderStatList = dtsOrderGoodsService.findOrderStat();
        StatVo statVo=new StatVo();
        String[] columns={"day","orders","products","amount"};
        statVo.setColumns(columns);
        List<Map> map=new ArrayList<>();
        for (OrderGoodsStat orderStat : orderStatList) {
            Map<String, Object> day=new HashMap<>();
            day.put("day",orderStat.getDay());
            day.put("orders",orderStat.getOrders());
            day.put("products",orderStat.getProducts());
            day.put("amount",orderStat.getAmount());
            map.add(day);
        }
        statVo.setRows(map);
        return ResponseUtil.ok(statVo);
    }

}
